package com.czp.ssm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.czp.ssm.bean.Company;
import com.czp.ssm.bean.Opera;
import com.czp.ssm.bean.OperaRule;
import com.czp.ssm.bean.Role;
import com.czp.ssm.bean.User;
import com.czp.ssm.bean.UserCard;
import com.czp.ssm.bean.UserCardExample;
import com.czp.ssm.bean.UserExample;
import com.czp.ssm.bean.UserExample.Criteria;
import com.czp.ssm.bean.UserRole;
import com.czp.ssm.dao.CompanyMapper;
import com.czp.ssm.dao.OperaMapper;
import com.czp.ssm.dao.OperaRuleMapper;
import com.czp.ssm.dao.RoleMapper;
import com.czp.ssm.dao.UserCardMapper;
import com.czp.ssm.dao.UserMapper;
import com.czp.ssm.dao.UserRoleMapper;
import com.czp.ssm.service.UserService;
import com.czp.ssm.utils.CommonUtils;
import com.czp.ssm.bean.Company;
import com.czp.ssm.bean.Role;
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserCardMapper userCardMapper;
	@Autowired
	private OperaMapper operaMapper;
	@Autowired
	private OperaRuleMapper operaRuleMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private RoleMapper roleMapper;
	
	public List<User> findUserByUserNameAndCompanyId(String COMPANY_ID, String USER_NAME) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("COMPANY_ID", COMPANY_ID);
		map.put("USER_NAME", USER_NAME);
		List<User> userList = userMapper.selectUserByUserNameAndCompanyId(map);
		for (int i = 0; i < userList.size(); i++) {
			UserCardExample cardExample = new UserCardExample();
			com.czp.ssm.bean.UserCardExample.Criteria criteria = cardExample.createCriteria();
			criteria.andUserIdEqualTo(userList.get(i).getUserId());
			criteria.andCardTypeEqualTo("0");
			List<UserCard> selectByExample = userCardMapper.selectByExample(cardExample);
			if (selectByExample.size() > 0) {
				userList.get(i).setUserCard(selectByExample.get(0));
			}
			
		}
		return userList;
	}

	public List<User> findUserByCompanyId(String companyId) {
		List<User> userList = userMapper.selectUserByCompanyId(companyId);
		for (int i = 0; i < userList.size(); i++) {
			User user = userList.get(i);
			Map<String, String> map = new HashMap<String, String>();
			map.put("COMPANY_ID", companyId);
			map.put("USER_ID", user.getUserId());
			Opera opera = operaMapper.selectOperaPersonIdByCompanyIdAndUserId(map);
			OperaRule operaRule = operaRuleMapper.selectByOperaRuleId(opera.getOperaRuleId());
			user.setOperaRule(operaRule);
		}
		return userList;
	}

	public User findUserById(long id) {
		User user = userMapper.selectByPrimaryKey(id);
		Company company = companyMapper.selectByCompany(user.getCompanyId());
		user.setCompany(company);
		String roleId = userRoleMapper.selectRoleByUserId(user.getUserId());
		Role role = roleMapper.selectRoleByRoleId(roleId);
		user.setRole(role);
		return user;
	}

	public void register(User user) {
		String hashAlgorithmName = "MD5";
		Object credentials = user.getUserPassword();
		Object salt = ByteSource.Util.bytes(user.getUserPhone());;
		int hashIterations = 1024;
		Object result = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
		user.setUserId(CommonUtils.getOrderId());
		user.setUserPassword(result.toString());
		userMapper.insertSelective(user);
	}

	public List<User> findAllUser() {
		UserExample example = new UserExample();
		List<User> userList = userMapper.selectByExample(example);
		for (int i = 0; i < userList.size(); i++) {
			User user = userList.get(i);
			List<UserRole> userRoles = userRoleMapper.selectRoleListByUserId(user.getUserId());
			List<Role> roles = new ArrayList<Role>();
			for (int j = 0; j < userRoles.size(); j++) {
				UserRole userRole = userRoles.get(j);
				Role role = roleMapper.selectRoleByRoleId(userRole.getRoleId());
				roles.add(role);
			}
			user.setRoles(roles);
		}
		
		return userList;
	}

}
